package com.sduept.nwld.dataserver.controller.assessment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.fault.breaker.service.BreakerActionQueryManager;
import com.sduept.bigdata.fault.breaker.service.BreakerWaveletManager;
import com.sduept.bigdata.fault.dto.BreakerActionInfoDTO;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.LibTreeNode;

/**
 * 断路器动作熄弧分析
 * 
 * @author Ningfw
 * @date 2017-11-07
 * 
 */

@Named
@ViewScoped
public class BreakerWaveletController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1746086274190751808L;

	@Autowired
	private BreakerWaveletManager breakerWaveletManager;
	@Autowired
	private FaultManager fm;
	@Autowired
	private BreakerActionQueryManager baq;

	private TreeNode root;// 根节点
	private TreeNode currentSelect;

	private BreakerActionInfoDTO selectActionDTO;

	private Date startDate;
	private Date endDate;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@PostConstruct
	private void init() {
		initTree();
	}

	public void initTree() {
		endDate = new Date();
		startDate = new Date(endDate.getYear(), 0, 1);
		selectByDate();
	}

	public void genFaultTreeNode(List<FaultRecord> faultList) {
		for (FaultRecord fr : faultList) {
			new LibTreeNode(FaultRecord.class.getSimpleName(), fr, root);
		}
	}

	public void onNodeExpand(NodeExpandEvent event) {
		TreeNode currentNode = event.getTreeNode();
		if ("FaultRecord".equals(currentNode.getType())) {
			FaultRecord currentObj = (FaultRecord) currentNode.getData();
			Date faultTime = currentObj.getFaultTime();
			String equipmentId = currentObj.getEquipment_id();
			List<BreakerActionInfoDTO> breakerActInfo = baq.getDesignatedLineFaultTimeBreakerActionInfo(equipmentId,
					faultTime);
			for (BreakerActionInfoDTO b : breakerActInfo) {
				if ("跳闸".equals(b.getActionType())) {
					new LibTreeNode(BreakerActionInfoDTO.class.getSimpleName(), b, currentNode);
				}
			}
		}
	}

	// 事件
	public void onNodeSelect(NodeSelectEvent event) {
		currentSelect = event.getTreeNode();
		if (currentSelect != null) {
			if ("BreakerActionInfoDTO".equals(currentSelect.getType())) {
				selectActionDTO = (BreakerActionInfoDTO) currentSelect.getData();
			} else if ("FaultRecord".equals(currentSelect.getType())) {
				// 点击第一级打开故障报告
				selectActionDTO = null;
			}
		}
	}

	public void refresh() {
		selectByDate();
	}

	public void selectByDate() {
		if (startDate != null && endDate != null) {
			selectActionDTO = null;
			String start;
			String end;
			start = sdf.format(startDate);
			end = sdf.format(endDate).substring(0, 11) + "23:59:59";
			root = new DefaultTreeNode();
			List<FaultRecord> faultList = breakerWaveletManager.getBreakerFaultInfoByDate(start, end);
			genFaultTreeNode(faultList);
		} else {
			addErrorMessage("请输入要查询的起止日期！");
		}
	}

	public TreeNode getTreeNodeResource() {
		return root;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getCurrentSelect() {
		return currentSelect;
	}

	public void setCurrentSelect(TreeNode currentSelect) {
		this.currentSelect = currentSelect;
	}

	public BreakerActionInfoDTO getSelectActionDTO() {
		return selectActionDTO;
	}

	public void setSelectActionDTO(BreakerActionInfoDTO selectActionDTO) {
		this.selectActionDTO = selectActionDTO;
	}

	public String genString() {
		if (selectActionDTO != null)
			return "{\"breakerId\":" + selectActionDTO.getBreakerId() + ", " + "\"fileId\":\""
					+ selectActionDTO.getFaultFileId() + "\"," + "\"phase\": \"" + selectActionDTO.getPhase() + "\","
					+ "\"xhTime\":\"" + String.valueOf(selectActionDTO.getXhTimeInterval()) + "\"," + "\"actTime\": \""
					+ String.valueOf(selectActionDTO.getTwTime())
					/* + String.valueOf(selectActionDTO.getDistance0MomentsTime()) */
					+ "\", " + "\"faultTime\":\"" + selectActionDTO.getFaultTime() + "\"," + "\"lineName\":\""
					+ selectActionDTO.getLineName() + "\"," + "\"breakerName\":\"" + selectActionDTO.getBreakerName()
					+ "\", " + "\"removeTime\": \"" + String.valueOf(selectActionDTO.getAnalogFirstActionTime()) + "\","
					+ "\"faultReason\": \"" + selectActionDTO.getFaultReason() + "\"" + "}";
		return "";
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

}
